package com.lungle.warmdiary.app.view.active.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.StringUtils;
import com.lungle.warmdiary.app.view.me.view.BaseViewHolder;

import java.util.List;

public class ActiveImageAdapter extends BaseAdapter {

    private List<String> list;
    private Context mContext;
    private int max_num;


    public ActiveImageAdapter(Context context, List<String> list) {
        this.list = list;
        this.mContext = context;
    }

    public ActiveImageAdapter(Context mContext, List<String> list, int max_num) {
        this.list = list;
        this.mContext = mContext;
        this.max_num = max_num;
    }

    @Override
    public int getCount() {
        int listSize = list.size();
        if (max_num == 0)
            return listSize;
        return max_num < listSize ? max_num : listSize;
    }


    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.item_active_picture, parent, false);
        }
        ImageView picture = BaseViewHolder.get(convertView, R.id.iv_active_img);

        if (!TextUtils.isEmpty(list.get(position)))
            ImageLoader.getInstance().displayImage(StringUtils.getPreviewImageUrl(list.get(position)), picture);
        return convertView;
    }
}